/**
 * 
 */
package game;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JFrame;
import javax.swing.Timer;

/**
 * @author alexander fried
 * @author patrick stipsits
 */
public class Game extends JFrame implements WindowListener, WindowFocusListener, ActionListener {

	/**
	 * generated
	 */
	private static final long serialVersionUID = 7117270983890380406L;

	private MenuPanel menuPanel;
	private MusicPlayer musicplayer;

	public static int lastLevel = 0;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Game();
	}

	public Game() {

		Timer t = new Timer (100, this);
		t.start();

		// so far hard coded...
		int width = 800;
		int height = 600;

		this.menuPanel = new MenuPanel(this);
		this.add(menuPanel);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(width, height);
		setLocationRelativeTo(null);
		setTitle("Ice Cube");
		setResizable(false);
		setVisible(true);

		// remove this line to disable music
		 musicplayer = new MusicPlayer();

		addWindowListener(this);
		addWindowFocusListener(this);

	}
	
	/**
	 * saves the max. reached Level to HD
	 * */
	private void saveLastLevel(){
		FileOutputStream fos = null;
		ObjectOutputStream out = null;
		try {
			fos = new FileOutputStream("lastlevel");
			out = new ObjectOutputStream(fos);
			// out.writeObject(time);
			out.writeInt(lastLevel);
			out.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	
	/**
	 * close Game Ice Cube
	 * */
	public void exitIceCube(){
		saveLastLevel();
		System.exit(0);
	}

	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowClosed(WindowEvent e) {

	}

	public void windowClosing(WindowEvent e) {
		musicplayer.stopMusic();
		saveLastLevel();
	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	/**
	 * restore max reached Level
	 * */
	public void windowOpened(WindowEvent e) {
		FileInputStream fis = null;
		ObjectInputStream in = null;
		try {
			fis = new FileInputStream("lastlevel");
			in = new ObjectInputStream(fis);
			Game.lastLevel = in.readInt();
			in.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public void windowGainedFocus(WindowEvent e) {
		// System.out.println("FOCUS IS IN WINDOW");
		// validate();
		// menuPanel.validate();
		// menuPanel.repaint();
	}

	public void windowLostFocus(WindowEvent e) {
		// System.err.println("FOCUS IS NOT WINDOW");
		// validate();
		// menuPanel.validate();
		// menuPanel.repaint();
	}

	public void actionPerformed(ActionEvent e) {
		repaint();
		validate();
	}

	/**
	 * @return the musicplayer
	 */
	public MusicPlayer getMusicplayer() {
		return musicplayer;
	}

	/**
	 * @param musicplayer the musicplayer to set
	 */
	public void setMusicplayer(MusicPlayer musicplayer) {
		this.musicplayer = musicplayer;
	}

}
